package com.itcast.mallapplet.service.impl;

import com.itcast.applet.dto.AddressDTO;
import com.itcast.applet.dto.AddressyDTO;
import com.itcast.applet.entity.Address;
import com.itcast.context.BaseContext;
import com.itcast.exception.BaseException;
import com.itcast.mallapplet.mapper.AddressMapper;
import com.itcast.mallapplet.service.AddressService;

import com.itcast.result.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    //新增地址
    @Transactional
    public void addAddress(Address address) {
        if (address.getStatus() == 1){
            List<Address> addressList = addressMapper.queryAllAddress(BaseContext.getCurrentId());
            for (Address address1 : addressList) {
                if (address1.getStatus() == 1){
                    address1.setStatus(0);
                    addressMapper.amendAddress(address1);
                }
            }
        }
        addressMapper.insetAddAddress(address);
    }

    //查询单个地址
    public Address queryAddress(Long id) {
        Address address = addressMapper.queryAddress(id);
        return address;
    }

    //查询全部用户地址
    public List<Address> queryAllAddress() {
        Long userId = BaseContext.getCurrentId();
        List<Address> addressList = addressMapper.queryAllAddress(userId);
        return addressList;
    }

    //修改地址
    @Transactional
    public void amendAddress(Address address) {
        if (address.getStatus() == 1){
            List<Address> addressList = addressMapper.queryAllAddress(BaseContext.getCurrentId());
            for (Address address1 : addressList) {
                if (address1.getStatus() == 1){
                    address1.setStatus(0);
                    addressMapper.amendAddress(address1);
                }
            }
        }
        addressMapper.amendAddress(address);
    }

    //批量删除地址
    public void deleteAddressAll(List<Long> ids) {
        addressMapper.deleteAddressAll(ids);
    }


}
